<template>
  <div id="lineCharts" ref="lineCharts"></div>
</template>

<script>
import resize from '@/components/mixins/resize'

export default {
  title: '四件套飞机数据',
  name: 'EchartsQuartetAir',
  mixins: [resize],
  props: {
    datas: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    datas(val) {
      this.setOptions(val)
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$refs.lineCharts)
      this.setOptions(this.datas)
    },
    setOptions(datas = []) {
      const random = +(Math.random() * 60).toFixed(2);
      if (!this.chart) {
        return false
      }
      this.chart.setOption({
        colors: ['#0694ce', '#07ceac', '#FCFF00', '#E68E1C'],
        title: [
          {
            text: '',
            left: 'left',
            textStyle: {
              color: '#d1ecff',
              fontStyle: 'normal',
              fontWeight: '600',
              fontFamily: 'sans-serif',
              fontSize: 18
            }
          },
          {
            text: '',
            top: '50%',
            left: 'left',
            textStyle: {
              color: '#d1ecff',
              fontStyle: 'normal',
              fontWeight: '600',
              fontFamily: 'sans-serif',
              fontSize: 18
            }
          }
        ],
        tooltip: {
          trigger: 'axis',
          axisPointer: {type: 'cross'},
          padding: [5, 10],
          textStyle: {
            fontSize: 14
          }
        },
        grid: [
          {left: '5%', top: '5%', width: '60%', height: '38%'},
          {left: '5%', bottom: '5%', width: '60%', height: '38%'},
        ],
        xAxis: [
          {
            type: 'category',
            gridIndex: 0,
            boundaryGap: true,
            axisTick: {show: false},
            axisLabel: {color: '#fff', fontSize: 15},
            axisLine: {lineStyle: {color: '#0694ce'}},
            splitLine: {show: true, lineStyle: {color: '#0694ce'}},
            data: datas.map(item => {
              return item.date.split(' ')[1]
            }),
          },
          {
            type: 'time',
            gridIndex: 1,
            boundaryGap: false,
            axisTick: {show: false},
            axisLabel: {color: '#fff', fontSize: 15, showMaxLabel: true},
            axisLine: {lineStyle: {color: '#0694ce'}},
            splitLine: {show: true, lineStyle: {color: '#0694ce'}},
            splitNumber: 5,
            data: datas.map(item => {
              return item.date
            }),
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '',
            gridIndex: 0,
            nameTextStyle: {color: '#fff', fontSize: 15},
            axisTick: {show: false},
            splitArea: {show: false},
            axisLabel: {color: '#fff', fontSize: 14, showMaxLabel: true},
            axisLine: {lineStyle: {color: '#0694ce'}},
            splitLine: {show: true, lineStyle: {color: '#0694ce'}},
            min: 0,

          },
          {
            type: 'value',
            name: '',
            gridIndex: 1,
            nameTextStyle: {color: '#fff', fontSize: 15},
            axisTick: {show: false},
            splitArea: {show: false},
            axisLabel: {color: '#fff', fontSize: 14, showMaxLabel: true},
            axisLine: {lineStyle: {color: '#0694ce'}},
            splitLine: {show: true, lineStyle: {color: '#0694ce'}},
            min: 0,
            splitNumber: 5
          }
        ],
        series: [
          {
            name: '电压',
            type: 'bar',
            barWidth: 5,
            xAxisIndex: 0,
            yAxisIndex: 0,
            data: datas.map(item => {
              return item.voltage
            }),
            itemStyle: {
              normal: {
                color: '#0694ce'
              }
            }
          },
          {
            name: '电流',
            type: 'line',
            xAxisIndex: 1,
            yAxisIndex: 1,
            data: datas.map(item => {
              return [item.date, item.current]
            })
          },

          {
            name: '阻力',
            type: 'gauge',
            center: ['85%', '25%'],
            radius: '40%',
            axisLine: {
              lineStyle: {
                width: 20,
                color: [
                  [0.3, '#67e0e3'],
                  [0.7, '#37a2da'],
                  [1, '#fd666d']
                ]
              }
            },
            pointer: {itemStyle: {color: 'auto'}},
            axisTick: {
              distance: -20,
              length: 8,
              lineStyle: {color: '#fff', width: 2}
            },
            splitLine: {
              distance: -20,
              length: 30,
              lineStyle: {color: '#fff', width: 4}
            },
            axisLabel: {color: 'auto', distance: 25, fontSize: 18},
            detail: {
              valueAnimation: true,
              formatter: '{value}',
              color: 'auto'
            },
            data: [
              {
                value: datas.length ? datas[datas.length - 1].drag : +(Math.random() * 100).toFixed(2)
              }
            ]
          },

          // 温度
          {
            name: '温度',
            type: 'gauge',
            center: ['85%', '75%'],
            radius: '35%',
            min: 0,
            max: 100,
            itemStyle: {
              color: '#FFAB91'
            },
            progress: {show: true, width: 20},
            pointer: {show: false},
            axisLine: {lineStyle: {width: 20}},
            axisTick: {
              distance: -30,
              splitNumber: 5,
              lineStyle: {width: 2, color: '#fff'}
            },
            splitLine: {
              distance: -52,
              length: 14,
              lineStyle: {width: 3, color: '#fff'}
            },
            axisLabel: {distance: -20, color: '#fff', fontSize: 18},
            anchor: {show: false},
            title: {show: false},
            detail: {
              valueAnimation: true,
              formatter: '{value} °C',
              color: 'auto'
            },
            data: [{value: datas.length ? datas[datas.length - 1].temperAture : random}]
          },
          {
            type: 'gauge',
            center: ['85%', '75%'],
            radius: '35%',
            min: 0,
            max: 100,
            itemStyle: {color: '#FD7347'},
            progress: {show: true, width: 8},
            pointer: {show: false},
            axisLine: {show: false},
            axisTick: {show: false},
            splitLine: {show: false},
            axisLabel: {show: false},
            detail: {show: false},
            data: [{value: datas.length ? datas[datas.length - 1].temperAture : random}]
          }
        ]
      });
    }
  }
};
</script>

<style lang="less" scoped>
  #lineCharts {
    width: 100%;
    height: 100%;
  }
</style>